# coding:utf-8

from django.db import models
from django.contrib.auth.models import User
from gettext import gettext as _

class NewsGroup(models.Model):
    slug = models.SlugField( _(u'Системное имя'), max_length=100, blank=True)
    name = models.CharField( _(u'Название группы'), max_length=200)
    owner = models.ForeignKey(User, verbose_name=_(u"Владелец"), related_name="forms")

    class Meta:
        verbose_name = _(u"Группа новостей")
        verbose_name_plural = _(u"Группы новостей")

    def __unicode__(self):
        return self.name


class NewsManager(models.Manager):
    def get_news(self, owner, group=None, max=5):
        return self.get_query_set().filter(owner=owner, group=group)[0:max]

class News(models.Model):
    title = models.CharField( _(u'Заголовок'), max_length=200)
    teaser = models.TextField( _(u'Анонс'), blank=True)
    content = models.TextField( _(u'Текст'), blank=True)
    owner = models.ForeignKey(User, verbose_name=_(u"Владелец"), related_name="forms")
    group = models.ForeignKey(NewsGroup, verbose_name=_(u"Группа"), related_name="forms", blank=True, null=True)
    public = models.BooleanField( _(u"Опубликована"), default=True)
    create_date = models.DateField(_(u"Дата создания"), auto_now_add=True, editable=False )
    modify_date = models.DateField(_(u"Дата изменения"), auto_now=True, editable=False )

    object = NewsManager()

    def __unicode__(self):
        return self.title

    def get_teaser(self):
        return self.teaser or self.content

    def get_content(self):
        return self.content or self.teaser

    class Meta:
        verbose_name = _(u"Новость")
        verbose_name_plural = _(u"Новости")
